import * as request from "../../../../services/CommonHttpService";
import { routerRedux } from 'dva/router';

export default ({
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {

    * getPhoneCode({payload}, {call, put}) {

      const {params, errorMsg, notice, server} = payload;

      try {
        let result = yield call(request.commonGet, '/user/sms', {}, params, server);
      } catch (error) {
        console.error("getPhoneCode", error);
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
      }

    },

    * register({payload}, {call, put}) {
      console.log(payload);
      const {params, errorMsg, notice, server} = payload;
      const values = {username: params.mobile, password: params.password, name: params.name, validcode: params.captcha};
      try {
        let result = yield call(request.commonFormPost, '/user', {}, values, server);
        !!notice && notice("success", "注册成功");
        yield put(routerRedux.replace('/sign/login'));
      } catch (error) {
        console.error("register", error);
        !!notice && notice("error", !!errorMsg ? errorMsg : error);
      }
    },
  },

  reducers: {

    registerHandle(state, {payload}) {
      return {
        ...state,
        status: payload.status,
      };
    },
  },
});
